package com.relife.common.utils;

import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class EmailUtil {

	public static void sendEmail(String title, String text, String email) {
		try {
			String emails[] = email.split(";");
			Address address[] = new Address[emails.length];
			for (int i = 0; i < emails.length; i++) {
				address[i] = new InternetAddress(emails[i]);
			}

			Properties properties = new Properties();
			properties.setProperty("mail.transport.protocol", "smtp");// 发送邮件协议
			properties.setProperty("mail.smtp.auth", "true");// 需要验证
	

			Session session = Session.getInstance(properties);
			session.setDebug(true);// debug模式
			// 邮件信息
			MimeMessage messgae = new MimeMessage(session);
			messgae.setFrom(new InternetAddress("1220600231@qq.com"));// 设置发送人
			messgae.setSubject(title);// 设置邮件主题
			messgae.setText(text);// 设置邮件内容
			messgae.setSentDate(new Date());
			// 发送邮件
			Transport tran = session.getTransport();
			tran.connect("smtp.qq.com", 465, "1220600231@qq.com", "yangweiwu@1989");// 连接到QQ邮箱服务器rpqkkrqyrubzfehf
			tran.sendMessage(messgae, address);// 设置邮件接收人
			tran.close();
		} catch (AddressException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		EmailUtil.sendEmail("测试", "测试", "448431377@qq.com");
	}
}
